package ace.module.upms.core.impl.controller.rest.admin.ext;

import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsUserAuthenticated;
import ace.cmp.util.model.dto.TreeNode;
import ace.module.security.api.AuthenticationApi;
import ace.module.upms.api.constant.UpmsConstant;
import ace.module.upms.core.impl.dao.entity.UpmsMenu;
import ace.module.upms.core.impl.service.UpmsMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜单 admin controller.
 *
 * @author Caspar
 */
@Tag(name = "菜单-管理-API")
@PreAuthorizeIsUserAuthenticated
@AllArgsConstructor
@RestController
@RequestMapping(UpmsMenuExtAdminController.PATH)
@Valid
public class UpmsMenuExtAdminController {
  public static final String PATH = "/upms/admin/upms-menu/v1";
  public static final String URI_GET_ALL_BY_CURRENT_UPMS_APP_ID = "/getAllByCurrentUpmsAppId";
  public static final String URL_GET_ALL_BY_CURRENT_UPMS_APP_ID =
      PATH + URI_GET_ALL_BY_CURRENT_UPMS_APP_ID;
  public static final String URI_GET_TREE_ALL_BY_CURRENT_UPMS_APP_ID =
      "/getTreeAllByCurrentUpmsAppId";
  public static final String URL_GET_TREE_ALL_BY_CURRENT_UPMS_APP_ID =
      PATH + URI_GET_TREE_ALL_BY_CURRENT_UPMS_APP_ID;
  public static final String URI_GET_ALL_BY_OUT_TYPE_AND_OUT_ID = "/getAllByOutTypeAndOutId";
  public static final String URL_GET_ALL_BY_OUT_TYPE_AND_OUT_ID =
      PATH + URI_GET_ALL_BY_OUT_TYPE_AND_OUT_ID;
  public static final String MENU_ADMIN_READ = UpmsConstant.MODULE_NAME + "_menu_admin_read";
  public static final String MENU_ADMIN_WRITE = UpmsConstant.MODULE_NAME + "_menu_admin_write";
  private final UpmsMenuService upmsMenuService;
  private final AuthenticationApi authenticationApi;

  @Operation(summary = "查询全部菜单,根据当前账户upmsAppId", description = "权限：" + MENU_ADMIN_READ)
  @PreAuthorize("@securityService.hasAuthority('" + MENU_ADMIN_READ + "')")
  @GetMapping(URI_GET_ALL_BY_CURRENT_UPMS_APP_ID)
  public R<List<UpmsMenu>> getAllByCurrentUpmsAppId() {
    Long upmsAppId = authenticationApi.getCurrentUpmsAppId();
    List<UpmsMenu> result = this.upmsMenuService.getAllByUpmsAppId(upmsAppId);
    return R.ok(result);
  }

  @Operation(summary = "查询全部树开菜单,根据当前账户upmsAppId", description = "权限：" + MENU_ADMIN_READ)
  @PreAuthorize("@securityService.hasAuthority('" + MENU_ADMIN_READ + "')")
  @GetMapping(URI_GET_TREE_ALL_BY_CURRENT_UPMS_APP_ID)
  public R<List<TreeNode<UpmsMenu>>> getTreeAllByCurrentUpmsAppId() {
    Long upmsAppId = authenticationApi.getCurrentUpmsAppId();
    List<TreeNode<UpmsMenu>> result = this.upmsMenuService.getTreeAllByUpmsAppId(upmsAppId);
    return R.ok(result);
  }

  @Operation(summary = "查询全部树开菜单,根据外部应用键值", description = "权限：" + MENU_ADMIN_READ)
  @PreAuthorize("@securityService.hasAuthority('" + MENU_ADMIN_READ + "')")
  @GetMapping(URI_GET_ALL_BY_OUT_TYPE_AND_OUT_ID)
  public R<List<TreeNode<UpmsMenu>>> getAllByOutTypeAndOutId(
      @Valid @NotEmpty @RequestParam(value = "outType") String outType,
      @Valid @NotEmpty @RequestParam(value = "outId") String outId) {
    List<TreeNode<UpmsMenu>> result = this.upmsMenuService.getAllByOutTypeAndOutId(outType, outId);
    return R.ok(result);
  }
}
